#!/usr/local/bin/perl
#
# $Id: checkHash.pl,v 1.1 2009/09/22 01:08:06 rdilley Exp $
#
# author: ron dilley
#
# desc: this perl script compares pot files
#
# Copyright (C) 2009  Ron A. Dilley
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2
# of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# ron.dilley@uberadmin.com
#
############################################################################

#
# modules
#
use Getopt::Std;

#
# pragmas
#
use strict;

#
# set environment
#
$ENV{PATH} = "/usr/bin:/bin:/usr/sbin:/sbin:/usr/ucb";

#
# turn on autoflush
#
select STDERR; $| = 1;
select STDOUT; $| = 1;

#
# defines
#
$::TRUE = 1;
$::FALSE = 0;
$::FAILED = -1;

$::VERSION = '$Id: checkHash.pl,v 1.1 2009/09/22 01:08:06 rdilley Exp $';
$::PROGNAME = "checkHash.pl";

%::Config = ();
$::Config{'debug'} = $::FALSE;

#
# main routine
#
if ( &main() != $::TRUE ) {
  exit( 1 );
}

exit( 0 );

############################################################################
#
# sub-routines
#

#
# main routine
#
sub main {
  my $arg;

  #
  # display script banner
  #
  &show_banner();

  #
  # parse command-line
  #
  &parse_command_line();

  # process args that are left
  while( $arg = shift( @::ARGV ) ) {
    processPotFile( $arg );
  }

  # done
  return $::TRUE;
}

#
# display banner info
#
sub show_banner {
  print stderr "$::PROGNAME v$::VERSION\n";
  print stderr "By: Ron Dilley\n";
  print stderr "\n";
  print stderr "$::PROGNAME comes with ABSOLUTELY NO WARRANTY.\n";
  print stderr "This is free software, and you are welcome\n";
  print stderr "to redistribute it under certain conditions;\n";
  print stderr "See the GNU General Public License for details.\n";
  print stderr "\n";

  return $::TRUE;
}

#
# display help info
#
sub show_help {
  print stderr "Syntax:\n";
  print stderr "\n";
  print stderr "$::PROGNAME [options] {potfile} [{potfile} ...]\n";
  print stderr "\n";
  print stderr "-d {0-9}   Display debug information during program run\n";
  print stderr "\n";

  return $::TRUE;
}

#
# parse command-line arguments
#
sub parse_command_line {
  no strict 'vars';

  if ( getopts( 'd:' ) == $::FALSE ) {
    &show_help();
    return $::FAILED;
  }
  if ( defined $opt_d ) {
    if ( $opt_d > 0 ) {
      # set debug mode
      $::Config{'debug'} = $opt_d;
    }
  }

  return $::TRUE;
}

#
# process and compare data from pot files
#
sub processPotFile {
  my ( $fname ) = @_;
  my $line;

  if ( ! defined open( INFILE, $fname ) ) {
    print stderr "ERR - Unable to open file [$fname]\n";
    return $::FAILED;
  }

  while( $line = <INFILE> ) {
    chomp( $line );
    if ( $line =~ m/^(\$NT\$.{32,32})\:(.*)$/ ) {
      if ( exists $::hashes{$1} ) {
	if ( $2 eq $::hashes{$1} ) {
	  # two hashes and plaintexts match
	  print "MATCH $1:$2\n";
	} else {
	  # no match, bad hash or hash collision
	  print "COLLISION $1:$::hashes{$1} != $2\n";
	}
      } else {
	$::hashes{$1} = $2;
      }
    } else {
      # unknown line format in pot file
    }
  }

  close( INFILE );

  return $::TRUE;
}
